import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from "@vitejs/plugin-vue-jsx";
import path from 'path'

//https://vue-macros.dev/features/reactivity-transform.html
/*
ref -> $ref
computed -> $computed
shallowRef -> $shallowRef
customRef -> $customRef
toRef -> $toRef
 */
import ReactivityTransform from '@vue-macros/reactivity-transform/vite'
import AutoImport from 'unplugin-auto-import/vite'
import {loadEnv} from 'vite'
import langTag from 'lang-vite-plugin'
import customVite from 'custom-vite-plugin'

var lang = process.argv.filter(it =>it.startsWith("lang=")).map(it=>it.slice("lang=".length))[0] || "cn";
process.env.VITE_APP_LANG = lang

console.log("╭───────────────────────────────────────────────────────╮");
console.log("│             lang-vite-plugin 当前语言：" + process.env.VITE_APP_LANG + "             │");
console.log("╰───────────────────────────────────────────────────────╯");

// https://vitejs.dev/config/
export default ({mode}) => {
    const env = loadEnv(mode, process.cwd())

    var vPath = env.VITE_APP_V_PATH;
    if (!vPath) {
        vPath = env.VITE_APP_APP_CODE;
        if (!vPath.startsWith("/")) {
            vPath = "/" + vPath;
        }
    }

    return defineConfig({
        base: vPath || "/",
        server:{
            host: 'localhost',
            port: 3001
        },
        build: {
            rollupOptions: {
                plugins: []
            }
        },
        resolve: {
            alias: {
                "@": path.resolve("./src") // 相对路径别名配置，使用 @ 代替 src
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    api: 'modern-compiler'
                }
            },
        },
        plugins: [
            vue(),
            vueJsx(),
            AutoImport({
                imports: ['vue', 'vue-router'], //自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入
                dts: false, // 生成在src路径下名为auto-import.d.ts的声明文件
            }),
            ReactivityTransform(),  //vue3 宏
            customVite(env),    //自定义vue文件处理，添另 data-bind-exp,data-chk-exp 属性
            langTag(env)        //自定义语言标签处理。
        ],
    })

};
